package czh.cocoa.ui;

import czh.cocoa.foundation.NSArray;
import czh.cocoa.foundation.NSMutableArray;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.ui.impl.UITabBar;
import czh.cocoa.ui.impl.UITabBarDelegate;
import czh.cocoa.ui.impl.UITabBarItem;

public class UITabBarController extends UIViewController implements UITabBarDelegate {

	private UITabBar mTabBar;
	
	private NSMutableArray mViewControllers;
	
	private UIViewController mSelectedViewController;
	
	public UITabBarController() {
		mTabBar = new UITabBar();
		mTabBar.setDelegate(this);
		mViewControllers = new NSMutableArray(4);
	}
	
	public UITabBar tabBar() {
		return mTabBar;
	}
	
	public void viewDidLoad() {
		UIView view = view();
		CGRect bounds = view.bounds();
		
		CGRect barframe = mTabBar.frame();
		barframe.origin.y = bounds.size.height - barframe.size.height;
		barframe.size.width = bounds.size.width;
		mTabBar.setFrame(barframe);
		mTabBar.setAutoresizingMask(UIView.UIViewAutoresizingFlexibleTopMargin);
		view.addSubview(mTabBar);
		
		if (mSelectedViewController != null) { 
			UIView content_view = mSelectedViewController.view();
			content_view.setFrame(getViewControllerFrame());
			view.addSubview(content_view);
		}
	}
	
	private CGRect getViewControllerFrame() {
		CGRect bounds = view().bounds();
		CGRect barframe = mTabBar.frame();
		bounds.size.height -= barframe.size.height;
		return bounds;
	}
	
	public void setViewControllers(NSArray controllers) {
		final UIViewController ctrl0 = mSelectedViewController;
		if (ctrl0 != null && ctrl0.isViewLoaded()) { 
			ctrl0.view().removeFromSuperview();
		}
		
		for (NSObject o : mViewControllers) {
			UIViewController ctrl = (UIViewController) o;
			ctrl.setTabBarController(null);
		}
		mViewControllers.removeAllObjects();
		
		mTabBar.setItems(null);
		
		if (controllers != null) {
			for (NSObject o : controllers) {
				UIViewController ctrl = (UIViewController) o;
				ctrl.setTabBarController(this);
			}
			mViewControllers.addObjectsFromArray(controllers);
			
			UIViewController ctrl = (UIViewController) controllers.firstObject();
			if (ctrl != null && isViewLoaded()) {
				UIView content_view = ctrl.view();
				content_view.setFrame(getViewControllerFrame());
				view().addSubview(content_view);
			}
			
			NSArray items = getTabBarItems(controllers);
			mTabBar.setItems(items);
			
			UITabBarItem item = (UITabBarItem)items.firstObject();
			mTabBar.setSelectedItem(item);
			
		} else {
			
			mSelectedViewController = null;
			
		}
	}
	
	private NSArray getTabBarItems(NSArray controllers) {
		int count = controllers.count();
		NSMutableArray items = new NSMutableArray(count);
		for (int i = 0; i < count; i++) {
			UIViewController ctrl = (UIViewController)controllers.objectAtIndex(i);
			items.addObject(ctrl.tabBarItem());
		}
		return items;
	}
	
	public NSArray viewControllers() {
		return new NSArray(mViewControllers);
	}
	
	public void setSelectedIndex(int index) {
		final NSArray controllers = mViewControllers;
		final UIViewController ctrl0 = mSelectedViewController;
		int index0 = controllers.indexOfObject(ctrl0);
		if (index0 != index) {
			if (ctrl0 != null && ctrl0.isViewLoaded()) {	
				ctrl0.view().removeFromSuperview();
			}
			if (index >= 0 && index < controllers.count()) {
				UIViewController ctrl = (UIViewController) controllers.objectAtIndex(index);
				mSelectedViewController = ctrl;
				if (isViewLoaded()) {
					UIView content_view = ctrl.view();
					content_view.setFrame(getViewControllerFrame());
					view().addSubview(content_view);
				}
			} else {
				mSelectedViewController = null;
			}
		}
	}
	
	public int selectedIndex() {
		int index = mViewControllers.indexOfObject(mSelectedViewController);
		return index != NSNotFound ? index : -1;
	}
	
	public void setSelectedViewController(UIViewController ctrl) {
		int index = mViewControllers.indexOfObject(ctrl);
		setSelectedIndex(index);
	}
	
	public UIViewController selectedViewController() {
		return mSelectedViewController;
	}

	@Override
	public void tabBarDidSelectItem(UITabBar tabBar, UITabBarItem item) {
		int index = -1;
		for (int i = 0; i < mViewControllers.count(); i++) {
			UIViewController ctrl = (UIViewController) mViewControllers.objectAtIndex(i);
			if (ctrl.tabBarItem() == item) {
				index = i;
				break;
			}
		}
		setSelectedIndex(index);
	}
	
}
